function Alter_Head(is_set){
    for (let i = 0; i < 5; i++) {
        if (Open_set()) {
            if (is_set) {
                //修改头像
                var res2 = Alter_head();
                if (res2) {
                    return true;
                }else {
                    back();
                    sleep(1000);
                    back();
                    sleep(1000);
                }
            }else {
                //设置头像
                var res3 = Open_photo();
                if (res3) {
                    return true;
                }else {
                    back();
                    sleep(1000);
                    back();
                    sleep(1000);
                }
            }
        }else {
            back();
            sleep(1000);
        }
    }
    return false;
}

function Open_set(){
    var menu = desc("Open navigation menu");
    if (has(menu)) {
        click(menu);
        logd("点击菜单");
        sleep(500);

        for (let i = 0; i < 5; i++) {
            var setting = text("Settings");
            if (has(setting)) {
                click(setting);
                logd("点击设置");
                sleep(1000);
                return true;
            }else {
                swipeToPoint(500, 800, 500, 300, 500);
                logd("没有找到设置...继续下拉");
                sleep(1000);
            }
        }
        return false;
    }else {
        toast("没有找到菜单按钮");
        return false;
    }
}

function Open_photo(){
    var set_photo = text("Set Profile Photo");
    if (has(set_photo)) {
        click(set_photo);
        logd("点击设置头像");
        sleep(1000);
        return Set_head();
    }else {
        toast("没有找到Set Profile Photo");
        return false;
    }
}

function Set_head(){
    var set_head = text("Use an emoji");
    if (has(set_head)) {
        click(set_head);
        logd("点击进行头像选择");
        sleep(1000);

        var title = text("Choose emoji or sticker").getOneNodeInfo(500);
        var photo_list = title.nextSiblings()[0].child(0).child(2).child(1).child(0).allChildren();
        var m = random(1,24);
        photo_list[m].click();
        logd("选择第"+m+"个头像");
        sleep(1500);

        var finish = text("Set Profile Photo");
        if (has(finish)) {
            click(finish);
            logd("点击确定完成头像选择");
            sleep(1000);
            back();
            sleep(1000);
            return true;
        }else {
            toast("没有找到确定按钮");
        }
    }else {
        toast("没有找到头像选择按钮");
    }
}

function Alter_head(){
    var change_photo = desc("Change profile photo");
    if (has(change_photo)) {
        click(change_photo);
        logd("点击修改头像按钮");
        sleep(1000);

        var result = Set_head();
        if (result) {
            logd("头像修改成功");
            sleep(1000);
            back();
            return true;
        }
    }
}

// Alter_Head(false);
